//
//  ByViewScroeBillListTable.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/24.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewScroeBillListTable.h"

// cell
#import "ByCellScroeBillListItem.h"



@interface ByViewScroeBillListTable() <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView   *table;
@property (nonatomic, strong) UIView        *header;

@end


@implementation ByViewScroeBillListTable
{
    NSInteger _items;
}

- (UIView *)header {
    
    if (!_header) {
        _header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        _header.backgroundColor = self.table.backgroundColor;
        
        // 线条
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, _header.height - 1, _header.width, 1)];
        line.backgroundColor = BY_LINE_COLOR;
        [_header addSubview:line];

        // 文字
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _header.width - 10, _header.height)];
        contentLabel.font = FONT(16);
        contentLabel.textAlignment = NSTextAlignmentRight;
        contentLabel.tag = 1;
        [_header addSubview:contentLabel];
    }
    return _header;
}
- (void)setupUI {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.backgroundColor = BY_BG_COLOR;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self addSubview:self.table];
    
    self.table.tableHeaderView = self.header;
}
- (void)setFrame:(CGRect)frame {
    
    [super setFrame:frame];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}
- (void)setIndex:(NSInteger)index {
    
    _index = index;
    
    // 设置头视图的内容
    UILabel *headerContentLabel = [self.header viewWithTag:1];
    headerContentLabel.textColor = index == 0 ? BY_GOLD_COLOR : BY_BLUE_COLOR;
    headerContentLabel.text = @"当前总收入12000蜜积分";
    
    __weak typeof(self) wself = self;
    if (_index == 0) {
        self.contentViewDidAppear = ^(NSInteger index) {
            
            _items = arc4random_uniform(5) + 1;
            [wself.table reloadData];
        };
    }
    else {
        self.contentViewDidAppear = ^(NSInteger index) {
            
            _items = arc4random_uniform(5) + 6;
            [wself.table reloadData];
        };
    }
    
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _items;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellScroeBillListItem *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[ByCellScroeBillListItem alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.amountColor = _index == 0 ? BY_GOLD_COLOR : BY_BLUE_COLOR;
    }
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 20)];
    label.backgroundColor = tableView.backgroundColor;
    label.adjustsFontSizeToFitWidth = true;
    label.numberOfLines = 1;
    label.text = @"  9月";
    label.font = FONT(18);
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor lightGrayColor];

    return label;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}


@end
